package cn.service.claim.impl;

import cn.dao.claim.ClaimVoucherDao;
import cn.dao.common.EmployeeDao;
import cn.entity.ClaimVoucher;
import cn.entity.Employee;
import cn.service.claim.ClaimVoucherBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service("claimVoucherBiz")
public class ClaimVoucherBizImpl implements ClaimVoucherBiz {
    @Autowired
    private ClaimVoucherDao claimVoucherDao;
    @Autowired
    private EmployeeDao employeeDao;
    public ClaimVoucherDao getClaimVoucherDao() {
        return claimVoucherDao;
    }

    @Override
    public List<ClaimVoucher> findBySnAndPage(Employee employee, int indexPage, int pageSize,final String status,final Date startDate,final Date endDate) {
        return claimVoucherDao.findBySnAndPage(employee,indexPage,pageSize,status,startDate,endDate);
    }

    public void setClaimVoucherDao(ClaimVoucherDao claimVoucherDao) {
        this.claimVoucherDao = claimVoucherDao;
    }

    @Override
    public long findCountBySn(Employee employee, final String status, final Date startDate, final Date endDate) {
        return claimVoucherDao.findCountBySn(employee,status,startDate,endDate);
    }

    @Override
    public ClaimVoucher findById(Long id) {
        return claimVoucherDao.findById(id);
    }

    @Override
    public void saveOrUpdate(ClaimVoucher claimVoucher) {
        //claimVoucher为报销单
        //如果状态为 已提交 那么修改下一个处理人为创建人的部门经理
        if ("已提交".equals(claimVoucher.getStatus())){
            claimVoucher.setNextDeal(employeeDao.findByPidAndDid(2,claimVoucher.getCreator().getSysDepartment().getId()));
        }else if ("打回".equals(claimVoucher.getStatus())){//如果状态为 打回 那么修改状态为已打回下一个处理人为创建人
            claimVoucher.setStatus("已打回");
            claimVoucher.setNextDeal(claimVoucher.getCreator());
        }else if ("通过".equals(claimVoucher.getStatus())){
            //如果状态为 通过 并且下一个审批人为财务 修改状态为已付款下一个处理人为null
            if (claimVoucher.getNextDeal().getSysPosition().getId()==4){
                claimVoucher.setStatus("已付款");
                claimVoucher.setNextDeal(null);
            }
            if (claimVoucher.getNextDeal()!=null) {
                //如果状态为 通过 并且总金额不大于5000或者下一个审批人为总经理 修改状态为已审批下一个处理人为财务
                if (claimVoucher.getNextDeal().getSysPosition().getId() == 3 || claimVoucher.getTotalAccount() < 5000) {
                    claimVoucher.setStatus("已审批");
                    claimVoucher.setNextDeal(employeeDao.findByPidAndDid(4, 4));
                }
                //如果状态为 通过 并且总金额大于等于5000 修改状态为待审批下一个处理人为总经理
                if (claimVoucher.getTotalAccount() >= 5000 && claimVoucher.getNextDeal().getSysPosition().getId() == 2) {
                    claimVoucher.setStatus("待审批");
                    claimVoucher.setNextDeal(employeeDao.findByPidAndDid(3, null));
                }
            }
        }else  if("拒绝".equals(claimVoucher.getStatus())){
            claimVoucher.setStatus("已终止");
            claimVoucher.setNextDeal(null);
        }
        claimVoucherDao.update(claimVoucher);
    }

    public void setEmployeeDao(EmployeeDao employeeDao) {
        this.employeeDao = employeeDao;
    }

    @Override
    public void delete(ClaimVoucher claimVoucher) {
        claimVoucherDao.delete(claimVoucher);
    }
}
